package cc.lpz.backend.mapper;

import cc.lpz.backend.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
    int add(User user);

    @Update("update user set username=#{username},password=#{password},birthday=#{birthday} where id=#{id}")
    int update(User user);

    @Delete("delete from user where id=#{id}")
    int delete(User user);

    @Select("select * from user where id=#{id}")
    User findById(int id);

    @Select("select * from user")
    List<User> findAll();

    @Select("select * from user where id=#{id}")
    @Results({
            @Result(property = "address",column = "id",
            one = @One(select = "cc.lpz.backend.mapper.AddressMapper.selectByUserId")),
            @Result(property = "orders",column = "id",
            many = @Many(select = "cc.lpz.backend.mapper.OrderMapper.selectByUserId")),
            @Result(property = "roles",column = "id",
            many = @Many(select = "cc.lpz.backend.mapper.RoleMapper.selectByUserId")),
            @Result(property = "userRoles",column = "id",
            many = @Many(select = "cc.lpz.backend.mapper.UserRoleMapper.selectByUserId"))
    })
    User selectById(@Param("id")int id);
}
